package com.ycy;

import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

public class RedisCURD {

  public static RedissonClient redissonClient = null;

  public static RLock getLock(String key){
    RLock reentrantLock = redissonClient.getLock(key);
    return reentrantLock;
  }

  public static void set(String key, String value, Integer expire) {
    RBucket<String> bucket = redissonClient.getBucket(key);
    // 过期时间，解决一致性问题 和 缓解redis空间问题
    bucket.set(value, expire, TimeUnit.MINUTES);
  }

  public static String get(String key, Integer expire) {
    RBucket<String> bucket = redissonClient.getBucket(key);
    String s = bucket.get();
    // 提高热点key的命中率
    bucket.expire(expire, TimeUnit.MINUTES);
    return s;
  }

  public static void main(String[] args) {
    Config config = new Config();
    config.useSingleServer().setAddress("redis://127.0.0.1:6379");
    RedissonClient redissonClient = Redisson.create(config);

    RBucket<String> bucket = redissonClient.getBucket("x");
    bucket.set("xxx");

    redissonClient.shutdown();

  }

}
